package conmon

import (
	"github.com/dgrijalva/jwt-go"
	"log"
	"time"
)

var Userid int

func GetJwtToken(secretKey string, iat, seconds int64, payload string) (string, error) {
	claims := make(jwt.MapClaims)
	claims["exp"] = iat + seconds
	claims["iat"] = iat
	claims["payload"] = payload
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims
	return token.SignedString([]byte(secretKey))
}
func ParseToken(tokenString string) (string, error) {
	token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
		return []byte("shangxinping"), nil
	})
	if err != nil {
		log.Println("token有问题", err)
		return "", nil
	}
	claims := token.Claims.(jwt.MapClaims)
	payload := claims["payload"].(string)
	exp := claims["exp"].(float64)
	if int(time.Now().Unix()) > int(exp) {
		log.Println("token已经过期")
		return "", nil
	}
	return payload, nil
}
